﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    public class MetrixDataBusiness
    {
        public static List<string> GetPartNoListByLineID(string lineId)
        {

            using (Production2Entities entity = new Production2Entities())
            {
                var query = from pN in entity.V_CAL_Part_Test_Series_Enable
                            where pN.line_id == lineId
                            select pN.part_id;
                return query.Distinct().OrderBy(p => p).ToList();
            }
            
        }

        public static List<string> GetLineList()
        {
            using (Production2Entities entity = new Production2Entities())
            {
                var query = from line in entity.V_CAL_Part_Test_Series_Enable
                            select line.line_id;
                return query.Distinct().OrderBy(p => p).ToList();
            }
        }

        public static List<TestPlanItem> GetTestPlanIDList(string lineId, string partNoId)
        {
            using (Production2Entities entity = new Production2Entities())
            {
                var query = from tP in entity.V_CAL_Part_Test_Series_Enable
                            where tP.line_id == lineId && tP.part_id == partNoId
                            select new TestPlanItem() { Description = tP.test_series_description, Value = tP.test_series_id };
                return query.Distinct().OrderBy(p => p.Description).ToList();
            }
        }

        public static bool IsRemark(string testSeriesID, string functionTestID)
        {
            using (Production2Entities entity = new Production2Entities())
            {
                var query = from tf in entity.V_CAL_Part_Functional_Test
                            where tf.test_series_id == testSeriesID && tf.functional_test_id == functionTestID 
                            && tf.latest_record == "N"
                            select tf.functional_test_id;
                return query.Count() > 0;
            }
        }

        public static List<TestPlanItem> GetFunctionalTestIDList(string testSeriesID)
        {
            using (Production2Entities eneity = new Production2Entities())
            {
                var query = from tf in eneity.V_CAL_Part_Functional_Test
                            where tf.test_series_id == testSeriesID
                            select new TestPlanItem() { Value = tf.functional_test_id, Description = tf.functional_test_description };
                return query.Distinct().OrderBy(p => p.Value).ToList();
            }
        }

        public static List<FunctionTestItem> GetFunctionalTestIDListByLine(string lineID)
        {
            using (Production2Entities entity = new Production2Entities())
            {
                var query = from tf in entity.V_CAL_PART_Line_Functional_Mapping
                            where tf.line_id == lineID
                            select new FunctionTestItem() { PartNoID = tf.part_id, TestPlanID = tf.test_series_id, FunctionalTestID = tf.functional_test_id, FunctionalTestDes = tf.functional_test_description, TestPlanDes = tf.test_series_description };
                return query.OrderBy(p=>p.PartNoID).ThenBy(p=>p.TestPlanDes).ThenBy(p=>p.FunctionalTestDes).ToList();
            }
        }
    }
}
